package com.wss.controller;

import com.alibaba.fastjson.JSON;
import com.wss.domain.Account;
import com.wss.domain.Message;
import com.wss.service.AccountService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * 帐户请求
 */
@Controller
@RequestMapping("/account")
public class AccountController {

    @Autowired
    private AccountService accountService;

    /**
     * 后台添加账户
     * @return
     */
    @RequestMapping(value="/addUser", method= RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public String addUser(Account account){
        System.out.println("==============进入到/addUser=============");
        String username = account.getUsername();
        // 此用户名是否已注册
        boolean userRegistered = accountService.checkUsername(username);
        // 返回给前端的信息对象
        Message message = new Message();
        String msg = "";
        if (!userRegistered){
            // 执行用户添加方法
            boolean addSuccess = accountService.addUser(account);
            if (addSuccess){
                message.setResCode(1);
                message.setResultMsg("添加成功");
            }else{
                message.setResCode(0);
                message.setResultMsg("添加失败");
            }
        }else{
            message.setResultMsg("用户名已存在");
        }
        msg = JSON.toJSONString(message);
        return msg;
    }

    /**
     * 后台修改账户
     * @return
     */
    @RequestMapping(value="/updateUser", method= RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public String updateUser(Account account){
        System.out.println("==============进入到/updateUser=============");
        // 此用户名是否已注册
        boolean userRegistered = accountService.checkOtherName(account);
        // 返回给前端的信息对象
        Message message = new Message();
        String msg = "";
        if (!userRegistered){
            // 执行用户修改方法
            boolean updateSuccess = accountService.updateUser(account);
            if (updateSuccess){
                message.setResCode(1);
                message.setResultMsg("修改成功");
            }else{
                message.setResCode(0);
                message.setResultMsg("修改失败，管理员唯一");
            }
        }else{
            message.setResCode(0);
            message.setResultMsg("用户名已存在");
        }
        msg = JSON.toJSONString(message);
        return msg;
    }

    /**
     * 普通用户修改用户名密码
     * @return
     */
    @RequestMapping(value="/updateUserInfo", method= RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public String updateUserInfo(Account account,HttpServletRequest request){
        System.out.println("==============进入到/updateUserInfo=============");
        // 拿到当前session对象
        HttpSession session = request.getSession();
        // 此用户名是否已注册
        boolean userRegistered = accountService.checkOtherName(account);
        // 返回给前端的信息对象
        Message message = new Message();
        String msg = "";
        if (!userRegistered){
            // 执行用户修改方法
            boolean updateSuccess = accountService.updateUserInfo(account);
            if (updateSuccess){
                message.setResCode(1);
                message.setResultMsg("修改成功");
                //修改完用户名密码清空session重新登陆一次
                session.removeAttribute("Account");
            }else{
                message.setResCode(0);
                message.setResultMsg("修改失败");
            }
        }else{
            message.setResCode(0);
            message.setResultMsg("用户名已存在");
        }
        msg = JSON.toJSONString(message);
        return msg;
    }

    /**
     * 删除账户
     * @return
     */
    @RequestMapping(value="/deleteUser", method= RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public String deleteUser(Account account){
        System.out.println("==============进入到/deleteUser=============");
        // 返回给前端的信息对象
        Message message = accountService.deleteUser(account);
        String msg = JSON.toJSONString(message);
        return msg;
    }

    /**
     * 修改管理员密码
     * @return
     */
    @RequestMapping(value="/changeAdminPwd", method= RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public String changeAdminPwd(@Param("oldPwd") String oldPwd, @Param("newPwd") String newPwd, HttpServletRequest request){
        System.out.println("==============进入到/changeAdminPwd=============");
        // 拿到当前管理员session对象
        HttpSession session = request.getSession();
        Account account = (Account) session.getAttribute("Account");
        // 原密码输入是否正确
        boolean checkOldPwd = accountService.checkAdminPwd(account,oldPwd);
        // 返回给前端的信息对象
        Message message = new Message();
        String msg = "";
        if (checkOldPwd){
            // 执行密码修改方法
            boolean changeSuccess = accountService.changeAdminPwd(account,newPwd);
            if (changeSuccess){
                message.setResCode(1);
                message.setResultMsg("修改成功");
                //修改完密码清空session重新登陆一次
                session.removeAttribute("Account");
            }else{
                message.setResCode(0);
                message.setResultMsg("修改失败");
            }
        }else{
            message.setResCode(0);
            message.setResultMsg("原密码输入错误");
        }
        msg = JSON.toJSONString(message);
        return msg;
    }
}
